//
//  LSRollingBallViewController.m
//  LookScore
//
//  Created by wr on 2018/3/20.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSRollingBallViewController.h"
#import "CustomSegmentedView.h"
#import "LSIntercontinentalMatchCell.h"
#import "LSRollingBallCell.h"
#import "LSLeagueCompetitionBaseVController.h"//联赛赛事
#import "LSRollingDetailViewController.h"
#import "LSRollModel.h"
#import "BattlefieldHeaderView.h"
#import "PublishIssueTopicViewController.h"
@interface LSRollingBallViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)NSMutableArray *modelArray;

@property(strong,nonatomic)NSArray *titlesArray;
@property (nonatomic, strong) NSMutableArray * recommendArray;
@property (nonatomic, strong) NSMutableArray * teamPercentageArray;
@property (nonatomic, strong) NSMutableArray * topicArray;
@end

@implementation LSRollingBallViewController

- (NSMutableArray *)recommendArray{
    if (!_recommendArray) {
        _recommendArray = @[].mutableCopy;
    }
    return _recommendArray;
}

- (NSMutableArray *)teamPercentageArray{
    
    if (!_teamPercentageArray) {
        _teamPercentageArray = @[].mutableCopy;
    }
    return _teamPercentageArray;
}

- (NSMutableArray *)topicArray{
    
    if (!_topicArray) {
        _topicArray = @[].mutableCopy;
    }
    return _topicArray;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
-(NSMutableArray *)modelArray{
    if(!_modelArray){
        _modelArray=[[NSMutableArray alloc]init];
    }
    return _modelArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftEqualToView(self.view)
    .topEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomEqualToView(self.view);
    self.titlesArray=@[@"看看推荐",@"专家分析",@"圈子"];
    self.tableView.tableHeaderView = [self tableHeadView];
    LSRefreshHeaderView *header = [LSRefreshHeaderView headerWithRefreshingBlock:^{
        [self loadData:YES];
    }];
    self.tableView.mj_header = header;// 设置header
    
    NSInteger issueButtonWidth=70;
    UIImage *issueImageName=[UIImage imageNamed:@"publish_issue_topic"];
    IssueTopicButton *issueTopicButton=[[IssueTopicButton alloc]init];
    //    issueTopicButton.frame=CGRectMake(LSScreenWidth-30-issueButtonWidth, tableView.height-30-issueButtonWidth, issueButtonWidth, issueButtonWidth);
    [issueTopicButton addTarget:self action:@selector(publishIssueTopicSender:) forControlEvents:UIControlEventTouchUpInside];
    [issueTopicButton setImage:issueImageName forState:UIControlStateNormal];
    issueTopicButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view insertSubview:issueTopicButton aboveSubview:self.tableView];
    issueTopicButton.sd_layout
    .rightSpaceToView(self.view, 10)
    .centerYEqualToView(self.view)
    .widthIs(issueButtonWidth)
    .heightIs(issueButtonWidth);
    
    [self loadData:YES];
}

- (void)loadData:(BOOL)refresh{
    
    
    [LSHttpEnclosureTool GET:ExpertCicle parameters:@{@"matchId":@(self.matchId)} resBlock:^(id responseObject) {
        MyLog(@"%@",responseObject);
        if (responseObject) {
            if (refresh) {
                [self.recommendArray removeAllObjects];
                [self.topicArray removeAllObjects];
                [self.teamPercentageArray removeAllObjects];
                [self.modelArray removeAllObjects];
            }
            
            for (NSDictionary * dict in responseObject[@"RecommendList"]) {
                LSRecommendModel * model = [LSRecommendModel mj_objectWithKeyValues:dict];
                [self.recommendArray addObject:model];
            }
            for (NSDictionary * dict in responseObject[@"TopicList"]) {
                LSTopicModel * model = [LSTopicModel mj_objectWithKeyValues:dict];
                [self.topicArray addObject:model];
            }
            
            [self.teamPercentageArray addObject:[LSRollModel mj_objectWithKeyValues:responseObject[@"TeamPercentage"]]];
            [self.modelArray addObjectsFromArray:@[self.teamPercentageArray,self.recommendArray,self.topicArray]];
            
            
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        
    }];
    
}

-(UIView *)tableHeadView{
    
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, 44)];
    backView.backgroundColor=LSTitleWhiteColor;
    
    
    CustomSegmentedView *aCustomSegmentedView=[[CustomSegmentedView alloc]initWidthCustomSegmentedViewFrame:CGRectMake(LSScareValue(160, LSScreenWidth)/2, 10, LSScreenWidth-LSScareValue(160, LSScreenWidth), 24) titlesArray:self.titlesArray seletedTitleColor:[UIColor whiteColor] normalTitleColor:LSSubColorFF9E44 seletedBgColor:LSSubColorFF9E44 normalBgColor:[UIColor whiteColor] bordeColor:LSSubColorFF9E44  borderWidth:1 seletedCustomViewIndex:^(NSInteger index) {
        if (self.modelArray && self.modelArray.count != 0) {
          NSArray * array = self.modelArray[index];
            if (array.count == 0) {
                
            }else{
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:index];
                [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }
        }
        
       
       
//        self.cellStyleFlag = index;
        
//        MyLog(@"选择第%zd板块",self.cellStyleFlag);
//        [self.controlArray removeAllObjects];
        [self.tableView reloadData];
    }];
    [aCustomSegmentedView selectedDefaultSegmentedIndex:0];
    [backView addSubview:aCustomSegmentedView];
    
    
    
    return backView;
    
}
#pragma mark 🔥 tableViewDelegate tableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.modelArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * array = self.modelArray[section];
    return array.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 110;
    }
    return 110;
}



- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        LSRollingBallCell *cell=[LSRollingBallCell RollingBallCellByTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray * array = self.modelArray[indexPath.section];
        cell.homeId = self.homeId;
        cell.guestId = self.guestId;
        cell.model = array[indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
        LSExpertAnalysisCell * cell = [LSExpertAnalysisCell LSExpertAnalysisCellByTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray * array = self.modelArray[indexPath.section];
        cell.model = array[indexPath.row];
        cell.block = ^(UIButton *sender) {
            if ([LSAccountArchive isLogin]) {
               [self attendBtnClicked:indexPath];
            }else{
                //跳到登录
            }
            
        };
        return cell;
        
    }else{
        
        LSLiveCircleCell * cell = [LSLiveCircleCell liveCircleCellByTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray * array = self.modelArray[indexPath.section];
        cell.model = array[indexPath.row];
        return cell;
    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section<3) {
        return 30;
    }
    return 0.01;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 30)];
    view.backgroundColor = LSBackGroundColor;
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, LSScreenWidth-20, 30)];
    label.font = LSFontSize12;
    label.textColor = LSTitleGrayColor1;
    [view addSubview:label];
    if (section == 0) {
        label.text = @"看看推荐";
    }else if (section == 1){
        label.text = @"专家分析";
        
    }else{
       label.text = @"圈子";
    }
    return view;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        NSArray * array = self.modelArray[indexPath.section];
        LSRecommendModel * model = array[indexPath.row];
        LSRollingDetailViewController * detailVC = [[LSRollingDetailViewController alloc] init];
        detailVC.Id = model.Id;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
   
    
}

- (void)attendBtnClicked:(NSIndexPath *)indexPath{
    
    NSArray * array = self.modelArray[indexPath.section];
    LSRecommendModel * model = array[indexPath.row];
    [LSHttpEnclosureTool POST:ExpertThumbup parameters:@{@"id":model.Id,@"cid":[LSAccountArchive account].Cid} resBlock:^(id responseObject) {
        if (responseObject) {
            [self loadData:YES];
        }
    }];
    
}
- (void)publishIssueTopicSender:(UIButton *)sender{
    
    if ([LSAccountArchive isLogin]) {
        PublishIssueTopicViewController *publishIssueVC=[[PublishIssueTopicViewController alloc]init];
        publishIssueVC.pushType = 1;
        publishIssueVC.selectMatchId = self.matchId;
        publishIssueVC.circleId = 1;
        //        KWeakSelf(self);
        //        publishIssueVC.block = ^{
        //            [weakself loadData:YES];
        //            [weakself loadTopicCircleDetailData];
        //        };
        [self.navigationController pushViewController:publishIssueVC animated:YES];
    }else{
        
        LSLoginVController *loginVC=[[LSLoginVController alloc]init];
        
        [self presentRootViewController:loginVC animated:YES completion:nil];
        loginVC.loginSuccessBlock = ^{
            
        };
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

